package com.qianfeng.smartdevices.security;

import com.qianfeng.smartdevices.event.UpdateUserTimeEvent;
import com.qianfeng.smartdevices.pojo.BaseUser;
import com.qianfeng.smartdevices.pojo.Menu;
import com.qianfeng.smartdevices.pojo.Role;
import com.qianfeng.smartdevices.service.MenusService;
import com.qianfeng.smartdevices.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class MySuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
    private RoleService roleService;

    private MenusService menusService;

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }
    @Autowired
    public void setMenusService(MenusService menusService) {
        this.menusService = menusService;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        System.err.println("登陆成功了");
        BaseUser baseUser = (BaseUser) authentication.getPrincipal();
        context.publishEvent(new UpdateUserTimeEvent(baseUser.getUsername()));
        System.err.println(baseUser);
        System.err.println(authentication.getCredentials());
        List<GrantedAuthority> grantedAuthorities= new ArrayList<>();
        Long uid = baseUser.getUid();
        List<Role> roles = roleService.findRoleByUserId(uid);
        roles.forEach(role -> {
            List<Menu> menuList = menusService.findMenuByRoleId(role.getId());
            role.setMenuList(menuList);
            grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_"+role.getName()));
            grantedAuthorities.addAll(menuList.stream().map(Menu::getPerms).map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
        });

        baseUser.setRoleList(roles);
        SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(baseUser,authentication.getCredentials(),grantedAuthorities));
        response.sendRedirect("/index.html");
    }
}
